package com.dongdongshop.controller;

import com.dongdongshop.data.Result;
import com.dongdongshop.enums.ResultEnum;
import com.dongdongshop.pojo.TbItemCat;
import com.dongdongshop.service.CatService;
import com.sun.org.apache.regexp.internal.RE;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("cat")
public class CatController {
    @DubboReference
    private CatService catService;

    @RequestMapping("toCat")
    public String toCat(){
        return "admin/item_cat";
    }
    @RequestMapping("listCat")
    @ResponseBody
    public Result listCat(@RequestParam(defaultValue = "0")Long parentId){
        try {
            List<TbItemCat> list = catService.listCat(parentId);
            if (list.size() == 0){
                return Result.response(ResultEnum.NOT_FOUND_lIST);
            }
            return Result.OK().setData(list);
        }catch (Exception e){
            return Result.ER();
        }
    }
    @RequestMapping("topType")
    @ResponseBody
    public Result topType(Long id){
        try {
            List<TbItemCat> list = catService.topType(id);
            return Result.OK().setData(list);
        }catch (Exception e){
            return Result.ER();
        }
    }
    @RequestMapping("addCat")
    @ResponseBody
    public Result addCat(TbItemCat tbItemCat){
        try {
            catService.addCat(tbItemCat);
            return Result.OK();
        }catch (Exception e){
            return Result.ER();
        }
    }
    @RequestMapping("delCats")
    @ResponseBody
    public Result delCats(@RequestParam(value = "ids")List<Long> ids){
        try {
            catService.delCats(ids);
            return Result.OK();
        }catch (Exception e){
            return Result.ER();
        }
    }

    @RequestMapping("toUpdate")
    @ResponseBody
    public Result toUpdate(Long id){
        try {
            TbItemCat tbItemCat = catService.toUpdate(id);
            return Result.OK().setData(tbItemCat);
        }catch (Exception e){
            return Result.ER();
        }
    }

    @RequestMapping("update")
    @ResponseBody
    public Result update(TbItemCat tbItemCat){
        try {
            catService.update(tbItemCat);
            return Result.OK();
        }catch (Exception e){
            return Result.ER();
        }
    }
}
